package v1

import (
	"gin-ebook/Models"
	"gin-ebook/pkg/e"
	"gin-ebook/pkg/util"
	"github.com/gin-gonic/gin"
)

// @Summary 新增文章
// @Produce  json
// @Tags articles
// @Param tagId query int true "标签id"
// @Param title query string true "文章标题"  maxlength(15)
// @Param desc query string true "文章描述"
// @Param content query string true "文章内容"
// @Param createdBy query string true "创建人"
// @Success 200 {string} string "{"code":200,"data":{},"msg":"ok"}"
// @Router /articles [post]

func AddArticle(tagId int, title string, desc string, content string, createdBy string, state int) (code int) {
	//
	//	if models.ExistTagByID(tagId) {
	//		data := make(map[string]interface{})
	//		data["tag_id"] = tagId
	//		data["title"] = title
	//		data["desc"] = desc
	//		data["content"] = content
	//		data["created_by"] = createdBy
	//		data["state"] = state
	//
	//		models.AddArticle(data)
	//		code = e.SUCCESS
	//		AddDynamic(title, "增加文章", createdBy)
	//	} else {
	//		code = e.ERROR_NOT_EXIST_TAG
	//	}
	return
}

// 删除文章
// @Summary 删除文章
// @Produce  json
// @Tags articles
// @Param id path int true "文章id"
// @Success 200 {string} string "{"code":200,"data":{},"msg":"ok"}"
// @Router /articles/{id} [delete]
func DeleteArticle(id int) (code int) {

	if models.ExistArticleByID(id) {
		models.DeleteArticle(id)
		code = e.SUCCESS
		AddDynamic(string(id), "删除文章", "布丁")
	} else {
		code = e.ERROR_NOT_EXIST_ARTICLE
	}
	return code

}

// @Summary 编辑文章
// @Produce  json
// @Tags articles
// @Param Id query int true "文章id"
// @Param title query string true "文章标题"  maxlength(15)
// @Param desc query string true "文章描述"
// @Param content query string true "文章内容"
// @Param createdBy query string true "修改人"
// @Success 200 {string} string "{"code":200,"data":{},"msg":"ok"}"
// @Router /articles/{id} [put]
func EditArticle(id int, tagId int, title string, desc string, content string, modifiedBy string) (code int) {
	//
	//	if models.ExistArticleByID(id) {
	//		if models.ExistTagByID(tagId) {
	//			data := make(map[string]interface{})
	//			if tagId > 0 {
	//				data["tag_id"] = tagId
	//			}
	//			if title != "" {
	//				data["title"] = title
	//			}
	//			if desc != "" {
	//				data["desc"] = desc
	//			}
	//			if content != "" {
	//				data["content"] = content
	//			}
	//
	//			data["modified_by"] = modifiedBy
	//
	//			models.EditArticle(id, data)
	//			code = e.SUCCESS
	//			AddDynamic(title, "编辑文章", modifiedBy)
	//
	//		} else {
	//			code = e.ERROR_NOT_EXIST_TAG
	//		}
	//	} else {
	//		code = e.ERROR_NOT_EXIST_ARTICLE
	//	}
	//
	return
}

// @Summary 获取单个文章
// @Produce  json
// @Tags articles
// @Param Id query int true "文章id"
// @Success 200 {string} string "{"code":200,"data":{},"msg":"ok"}"
// @Router /articles/{id} [get]
func GetArticle(id int) (code int, data models.Book) {

	if models.ExistArticleByID(id) {
		data = models.GetArticle(id)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST_ARTICLE
	}
	return
}
func GetBookDetail(id int) (code int, data models.Book) {

	if models.ExistArticleByID(id) {
		data = models.GetBookDetail(id)
		code = e.SUCCESS
	} else {
		code = e.ERROR_NOT_EXIST_ARTICLE
	}
	return
}

// @Summary 获取多个文章
// @Produce  json
// @Tags articles
// @Success 200 {string} string "{"code":200,"data":{},"msg":"ok"}"
// @Router /articles [get]
func GetArticles(c *gin.Context, maps map[string]interface{}) (code int, lists []models.Book, total int64) {
	page, pageSize := util.GetPage(c)
	lists = models.GetBooks(page, pageSize, maps)
	//total = models.GetArticleTotal(maps)
	code = e.SUCCESS
	return
}
func GetCategoryBooks(id int) (code int, lists []models.Book) {
	lists = models.GetCategoryBooks(id)
	//total = models.GetArticleTotal(maps)
	code = e.SUCCESS
	return
}

func GetHotArticles(c *gin.Context, maps map[string]interface{}) (code int, lists []models.Book, total int64) {
	page, pageSize := util.GetPage(c)
	lists = models.GetHotBooks(page, pageSize, maps)
	//total = models.GetArticleTotal(maps)
	code = e.SUCCESS
	return
}

func GetRecommendBooks(c *gin.Context, maps map[string]interface{}) (code int, lists []models.Book, total int64) {
	page, pageSize := util.GetPage(c)
	lists = models.GetRecommendBooks(page, pageSize, maps)
	//total = models.GetArticleTotal(maps)
	code = e.SUCCESS
	return
}
func SearchBook(title string) (code int, data []models.Book) {
	data = models.SearchBook(title)
	code = e.SUCCESS
	return
}
